package com.spzx.product.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.annotation.SpzxPrintLog;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
@Tag(name = "品牌管理模块")
public class BrandController extends BaseController {

    @Autowired
    BrandService brandService;

    @Operation(summary = "条件查询品牌列表")
    @RequiresPermissions(value = {"product:brand:list"})
    @RequestMapping("/list")
    public TableDataInfo list(Brand brand) {

        startPage();
        List<Brand> list = brandService.selectBrandList(brand);
        return getDataTable(list);
    }

    @Operation(summary = "查询所有品牌列表")
    @RequiresPermissions(value = {"product:brand:list"})
    @SpzxPrintLog(module = "查询品牌分页数据" ,isPrintResult = true)
    @GetMapping
    public AjaxResult listAll(){
        return success(brandService.listAll());
    }

    /*@Operation(summary = "删除品牌")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        //toAjax 可以根据增删改返回的影响记录的条数，返回成功或失败AjaxResult对象
        return toAjax(brandService.deleteBrand(id));
    }*/

    @Operation(summary = "批量删除品牌")
    @RequiresPermissions(value = {"product:brand:remove"})
    @Log(title = "删除品牌" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        //toAjax 可以根据增删改返回的影响记录的条数，返回成功或失败AjaxResult对象
        return toAjax(brandService.deleteBrands(ids));
    }

    //加了校验注解的类  必须再接收参数时指定Validated校验  才会校验
    @Operation(summary = "新增品牌")
    @RequiresPermissions(value = {"product:brand:add"})

    @PostMapping
    public AjaxResult add(@Validated @RequestBody Brand brand) {
        brand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(brandService.save(brand));
    }

    //先查询详情
    @Operation(summary = "查询品牌详情")
    @RequiresPermissions(value = {"product:brand:query"})
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return success(brandService.getById(id));
    }

    //前端对详情进行展示 修改 提交更新请求
    @Operation(summary = "更新品牌")
    @RequiresPermissions(value = {"product:brand:edit"})
    @PutMapping
    public AjaxResult edit(@RequestBody Brand brand) {
        brand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(brandService.edit(brand));
    }

}
